package com.iretailer.report.analyze.device.fieAnalysis.device;

import com.iretailer.report.analyze.annotation.AnalyzeClass;
import com.iretailer.report.analyze.commonUtil.ResponeResultType;
import com.iretailer.report.analyze.device.fieAnalysis.FileAnalyze;
import com.iretailer.report.util.ImageBase64;
import com.iretailer.report.util.StringUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.handler.codec.http.multipart.FileUpload;
import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder;
import io.netty.handler.codec.http.multipart.InterfaceHttpData;
import io.netty.handler.codec.http.multipart.MixedAttribute;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/3/10 18:41
 * @Description:
 */
@AnalyzeClass
@Component
public class Irc1600HeatMap extends FileAnalyze {

    private static final Logger log = LoggerFactory.getLogger(Irc1600HeatMap.class);
    @Value("${netty.path.heatmap}")
    private String heatmap;

    @Override
    public String[] analyzePath() {
        if (StringUtil.notEmpty(heatmap)) {
            return heatmap.split(",");
        } else {
            return new String[]{"/heatmap"};
        }
    }

    @Override
    public ResponeResultType fileInsert(Map<String, Object> paramsMap, HttpPostRequestDecoder decoder) {
        Map<String, Object> contentMap = transform2Map(decoder);
        if (!contentMap.isEmpty()) {
//            boolean result = updateHeatMap(contentMap);
            boolean result = false;
            if (result) {
                return ResponeResultType.getOkRequest(ResponeResultType.CFLType.CLOSE);
            }
        }
        return ResponeResultType.getBadRequest(ResponeResultType.CFLType.CLOSE);
    }

    private Map<String, Object> transform2Map(HttpPostRequestDecoder decoder) {
        Map<String, Object> itemMap = new HashMap<>();
        while (decoder.hasNext()) {
            InterfaceHttpData data = decoder.next();
            if (data != null) {
                try {
                    String name = data.getName();
                    InterfaceHttpData.HttpDataType dataType = data.getHttpDataType();
                    switch (dataType) {
                        case Attribute:
                            itemMap.put(name, ((MixedAttribute) data).getByteBuf().toString(CharsetUtil.UTF_8));
                            break;
                        case FileUpload:
                            ByteBuf byteBuf = ((FileUpload) data).getByteBuf();
                            ByteBufInputStream bbis = new ByteBufInputStream(byteBuf);
                            itemMap.put(name, ImageBase64.getImageStr(bbis));
                            break;
                    }
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                } finally {
                    data.release();
                }
            }
        }
        return itemMap;
    }


}
